<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\dao\setting;

use app\dao\BaseDao;
use app\model\setting\SystemAdmin;
use think\db\exception\DbException;

class SystemAdminDao extends BaseDao
{
    /**
     * 设置模型
     * @return string
     */
    protected function setModel(): string
    {
        return SystemAdmin::class;
    }

    /**
     * 获取某个管理员信息
     */
    public function getSystemAdminByAccount(string $account)
    {
        return $this->search(['account' => $account])->find();
    }

    /**
     * 员工列表
     * @param array $where
     * @param array $whereOr
     * @param array $order
     * @param int $page
     * @param int $listRows
     * @return array
     * @throws DbException
     */
    public function listSystemAdmin(array $where = [], array $whereOr = [], array $order = [], int $page = 1, int $listRows = 10): array
    {
        $listRows = [
            'query' => [], // url额外参数
            'fragment' => '', // url锚点
            'var_page' => 'page', // 分页变量
            'list_rows' => $listRows, // 每页数量
            'page' => $page // 第几页
        ];

        $query = $this->search($where);

        if (!empty($whereOr)) {
            $query->where(function ($or) use ($whereOr) {
                $or->whereOr($whereOr);
            });
        }

        $orderCondition = "sort desc, id asc";
        if (!empty($order)) {
            $prop = $order['prop'];
            $order = $order['order'] == 'descending' ? 'desc' : 'asc';
            $orderCondition = "$prop $order";
        }

        return $query->order($orderCondition)->paginate($listRows)->toArray();
    }
}
